﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DeviceCommon
{
    /// <summary>
    /// 设备基类，声明并实现了设备类一般公用的操作方法，如回复各种类型的请求
    /// </summary>
    public abstract class DeviceBase : IDevice
    {
        protected DeviceState _state = DeviceState.NotInitialized;

        protected bool _bExited = false;
        public bool Exited
        {
            get
            {
                return _bExited;
            }
        }

        public int TimeoutMillisecond
        {
            get;
            set;
        } = 120000;

        /// <summary>
        /// 指示设备的运行状态
        /// </summary>
        public DeviceState State
        {
            get
            {
                return _state;
            }
        }

        protected int _id = -1;
        public int ID
        {
            get
            {
                return _id;
            }
            set 
            {
                _id = value;
            }
        }

        public string Name
        {
            get
            {
                return this.GetType().Name;
            }
        }

        protected bool _bInited = false;
        public bool Inited
        {
            get
            {
                return _bInited;
            }
        }

        public IServer Server
        {
            get;
            set;
        }

        protected string _sessionID = "";
        public string SessionID
        {
            get
            {
                return _sessionID;
            }
            set
            {
                _sessionID = value;
            }
        }

        public IntPtr WindowHandle
        {
            get;
            set;
        }

        protected string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        public event WorkingCompletedHandler WorkingCompleted;
        protected virtual void OnWorkingCompleted(string outParams)
        {
            if (WorkingCompleted != null)
            {
                WorkingCompleted(outParams);
            }
        }

        public event WorkingFailedHandler WorkingFailed;
        protected virtual void OnWorkingFailed(string message)
        {
            if (WorkingFailed != null)
            {
                WorkingFailed(message);
            }
        }

        /// <summary>
        /// 单次动作执行失败的通知事件
        /// </summary>
        public event DoFailedHandler DoFailed = null;
        protected void OnDoFailed(string error)
        {
            if (DoFailed != null)
            {
                DoFailed(error);
            }
        }

        /// <summary>
        /// 动作执行超时事件（连续n时间超时）
        /// </summary>
        public event DoTimeoutHandler DoTimeout = null;
        protected void OnDoTimeout(string error)
        {
            if (DoTimeout != null)
            {
                DoTimeout(error);
            }
        }

        public void SetWorkingDoneAction(WorkingCompletedHandler handler0, WorkingFailedHandler handler1)
        {
            WorkingCompleted = new WorkingCompletedHandler(handler0);
            WorkingFailed = new WorkingFailedHandler(handler1);
        }

        public virtual bool Test(string param, out string outParam)
        {
            outParam = "";
            return true;
        }

        public abstract void Init(string inParam, out string outParam);

        public abstract bool DoAction(string param, out string outParam);

        public abstract void Start(string inParam, out string outParam);

        public abstract void Stop(string inParam, out string outParam);

        public abstract void Exit(string inParam, out string outParam);

        public abstract void ShowConfigDialog(string inParam);

        public void SendMessage(string msgPack)
        {

        }

        public string GetAssemblyDirectory()
        {
            Assembly ass = this.GetType().Assembly;
            string directory = Path.GetDirectoryName(ass.CodeBase);
            if (directory.StartsWith("file:\\"))
            {
                directory = directory.Substring(6);
            }
            return directory;
        }
    }
}
